package com.homedo.microservice.open.service.gateway.zuul.autoconfig;

import com.homedo.microservice.open.service.gateway.zuul.routelocator.LocalRouteLocator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.discovery.ServiceRouteMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@RibbonClients(defaultConfiguration = LocalRibbonClientDefaultConfig.DefaultRibbonConfig.class)
public class LocalRibbonClientDefaultConfig {
    @Autowired(required = false)
    private Registration registration;
    @Autowired
    private DiscoveryClient discovery;
    @Autowired
    private ServiceRouteMapper serviceRouteMapper;
    @Autowired
    protected ServerProperties server;
    @Autowired
    protected ZuulProperties zuulProperties;

//  参考 http://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.html
//    public static class BazServiceList extends ConfigurationBasedServerList {
//        public BazServiceList(IClientConfig config) {
//            super.initWithNiwsConfig(config);
//        }
//    }

    @Bean
    public DiscoveryClientRouteLocator discoveryRouteLocator() {
        return new LocalRouteLocator(this.server.getServletPrefix(),
                this.discovery, this.zuulProperties, this.serviceRouteMapper, this.registration);
    }


    @Configuration
    class DefaultRibbonConfig {
//        @Autowired
//        private PropertiesFactory propertiesFactory;
//
//        public static final int DEFAULT_CONNECT_TIMEOUT = 1000;
//        public static final int DEFAULT_READ_TIMEOUT = 1000;
//
//        @Value("${ribbon.client.name}")
//        private String name = "client";
//
//        @Bean
//        public IClientConfig ribbonClientConfig() {
//            DefaultClientConfigImpl config = new DefaultClientConfigImpl();
//            config.loadProperties(this.name);
//            config.set(CommonClientConfigKey.ConnectTimeout, DEFAULT_CONNECT_TIMEOUT);
//            config.set(CommonClientConfigKey.ReadTimeout, DEFAULT_READ_TIMEOUT);
//            return config;
//        }
//
//        @Bean
//        public IRule ribbonRule(IClientConfig config) {
//            if (this.propertiesFactory.isSet(IRule.class, name)) {
//                return this.propertiesFactory.get(IRule.class, config, name);
//            }
//            ZoneAvoidanceRule rule = new ZoneAvoidanceRule();
//            rule.initWithNiwsConfig(config);
//            return rule;
//        }
//
//        @Bean
//        public IPing ribbonPing(IClientConfig config) {
//            if (this.propertiesFactory.isSet(IPing.class, name)) {
//                return this.propertiesFactory.get(IPing.class, config, name);
//            }
//            return new DummyPing();
//        }
//
//        @Bean
//        @SuppressWarnings("unchecked")
//        public ServerList<Server> ribbonServerList(IClientConfig config) {
//            if (this.propertiesFactory.isSet(ServerList.class, name)) {
//                return this.propertiesFactory.get(ServerList.class, config, name);
//            }
//            ConfigurationBasedServerList serverList = new ConfigurationBasedServerList();
//            serverList.initWithNiwsConfig(config);
//            return serverList;
//        }
//
//        @Bean
//        public ServerListUpdater ribbonServerListUpdater(IClientConfig config) {
//            return new PollingServerListUpdater(config);
//        }
//
//        @Bean
//        @SuppressWarnings("unchecked")
//        public ServerListFilter<Server> ribbonServerListFilter(IClientConfig config) {
//            if (this.propertiesFactory.isSet(ServerListFilter.class, name)) {
//                return this.propertiesFactory.get(ServerListFilter.class, config, name);
//            }
//            ZonePreferenceServerListFilter filter = new ZonePreferenceServerListFilter();
//            filter.initWithNiwsConfig(config);
//            return filter;
//        }


    }

}
